package com.wwh.furns.web;

import com.wwh.furns.entity.Furn;
import com.wwh.furns.entity.Page;
import com.wwh.furns.service.FurnService;
import com.wwh.furns.service.impl.FurnServiceImpl;
import com.wwh.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Albert
 * @date 2024/7/26-16:16
 * @describe
 */
public class CustomerFurnServlet extends BasicServlet {
    private final FurnService furnService = new FurnServiceImpl();

    /**
     * 分页请求家具信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);

        Page<Furn> page = furnService.page(pageNo, pageSize);

        //把page放入request域
        req.setAttribute("page", page);

        //请求转发
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req, resp);
    }

    public void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String name = req.getParameter("name");
        if (name == null) name = "";
        Page<Furn> page = furnService.pageByName(pageNo, pageSize, name);

        //把page放入request域
        StringBuilder url = new StringBuilder("customerFurnServlet?action=pageByName");
        if(!name.isEmpty()){
            url.append("&name=").append(name);
        }
        page.setUrl(url.toString());
        req.setAttribute("page", page);

        //请求转发
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req, resp);
    }
}
